package org.ibase4j.service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ibase4j.core.base.BaseService;
import org.ibase4j.core.support.login.ThirdPartyUser;
import org.ibase4j.core.util.CacheUtil;
import org.ibase4j.core.util.SecurityUtil;
import org.ibase4j.mapper.PlatProUserMapper;
import org.ibase4j.mapper.SysUserThirdpartyMapper;
import org.ibase4j.model.PlatProUser;
import org.ibase4j.model.SysUserThirdparty;

import com.baomidou.mybatisplus.plugins.Page;

/**
 * SysUser服务实现类
 * 
 * @author ShenHuaJie
 * @version 2016-08-27 22:39:42
 */
@Service
@CacheConfig(cacheNames = "PlatUser")
public class PlatProUserService extends BaseService<PlatProUser> {
	@Autowired
	private SysUserThirdpartyMapper thirdpartyMapper;
	@Autowired
	private PlatProDicService platProDicService;
	@Autowired
	private SysDeptService sysDeptService;
	@Autowired
	private PlatProAuthorizeService platProAuthorizeService;
	@Autowired
	private PlatProUserMapper platProUserMapper;
	public PlatProUser queryById(Long id) {
		PlatProUser platProUser = super.queryById(id);
		/*if (platProUser != null) {
			if (platProUser.getDeptId() != null) {
				SysDept sysDept = sysDeptService.queryById(platProUser.getDeptId());
				if (sysDept != null) {
					platProUser.setDeptName(sysDept.getDeptName());
				} else {
					platProUser.setDeptId(null);
				}
			}
		}*/
		return platProUser;
	}
	/**
	 * 检查邮箱是否注册
	 */
	public PlatProUser isRegist(String email){
		if (email!=null&&!email.equals("")){
			return platProUserMapper.isRegist(email);
		}else {
			return null;
		}
	}

	/**
	 * 验证重置密码链接
	 * @param param
	 * @return
	 */
	public Integer validateURL(PlatProUser param){
		if(param.getAccount()!=null&&!param.getAccount().equals("")){
			return platProUserMapper.validateURL(param);
		}else{
			return 0;
		}
	}

	/**
	 * 发送邮件更改数据库uuid和发邮件时间
	 * @param params
	 */
	public void updateSendInfo(PlatProUser params){
		platProUserMapper.updateSendInfo(params);
	}
	/**
	 * 重置密码
	 * @param params
	 */
	public void resetPassword(PlatProUser params){
		platProUserMapper.resetPassword(params);
	}
	public Page<PlatProUser> query(Map<String, Object> params) {
		Map<String, String> userTypeMap = platProDicService.queryDicByType("USERTYPE");
		Page<PlatProUser> pageInfo = super.query(params);
		for (PlatProUser userBean : pageInfo.getRecords()) {
			if (userBean.getUserType() != null) {
				userBean.setUserTypeText(userTypeMap.get(userBean.getUserType()));
			}
			List<String> permissions = platProAuthorizeService.queryUserPermission(userBean.getId());
			for (String permission : permissions) {
				if (StringUtils.isBlank(userBean.getPermission())) {
					userBean.setPermission(permission);
				} else {
					userBean.setPermission(userBean.getPermission() + ";" + permission);
				}
			}
		}
		return pageInfo;
	}
	public void init() {
		List<Long> list = ((PlatProUserMapper) mapper).selectIdPage(Collections.<String, Object>emptyMap());
		for (Long id : list) {
			CacheUtil.getCache().set(getCacheKey(id), mapper.selectById(id));
		}
	}
}
